/*自身对应的.h文件*/
#include "app_gui.h"

/*程序内部对应的.h文件*/
#include "log.h"

/*外部库对应的.h文件*/
#include "lvgl.h"
#include "lv_port_disp_template.h"
#include "lv_port_indev_template.h"

#include "ui.h"
#include "ui_sysinfo.h"

/*系统.h文件*/
#include <unistd.h>
#include <pthread.h>

/***************************************************************
 * Name:	 app_gui_tick_thread()
 * Input : NULL
 * Output: void
 * Return: void
 * Author: hwl
 * Revise: V1.0
 * Description: LVGL心跳线程
 ***************************************************************/
void* app_gui_tick_thread(void* arg)
{
	while (1)
	{
		lv_tick_inc(1);
		usleep(1000);
	}
}

/***************************************************************
 * Name:	 app_gui_tick_init()
 * Input : NULL
 * Output: void
 * Return: void
 * Author: hwl
 * Revise: V1.0
 * Description: LVGL心跳线程初始化
 ***************************************************************/
static int app_gui_tick_init(void)
{
	pthread_t thread_id;
	pthread_attr_t attr;
	int res = 0;
	
	/*初始化线程属性*/
	res = pthread_attr_init(&attr);
	if (res != 0)
	{
		LOG_ERROR("pthread_attr_init fail\r\n");
		return -1;
	}

	/*设置为分离线程*/
	res = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
	if (res != 0)
	{
		LOG_ERROR("pthread_attr_setdetachstate failed\r\n");
		pthread_attr_destroy(&attr);
		return -1;
	}

	/*创建线程，线程为test_thread函数*/
	res = pthread_create(&thread_id, &attr, app_gui_tick_thread, NULL);
	if(res != 0)
	{
		LOG_ERROR("create thread fail\r\n");
		pthread_attr_destroy(&attr);
		return -1;
	}

	/*销毁线程属性对象*/
	pthread_attr_destroy(&attr);

	return 0;
}

/***************************************************************
 * Name:	 app_gui_poll_thread()
 * Input : NULL
 * Output: void
 * Return: void
 * Author: hwl
 * Revise: V1.0
 * Description: LVGL轮询线程
 ***************************************************************/
void* app_gui_poll_thread(void* arg)
{
	while (1)
	{
		lv_timer_handler();

		ui_sysinfo_update();

		usleep(5000);
	}
}

/***************************************************************
 * Name:	 app_gui_poll_init()
 * Input : NULL
 * Output: void
 * Return: void
 * Author: hwl
 * Revise: V1.0
 * Description: LVGL轮询线程初始化
 ***************************************************************/
static int app_gui_poll_init(void)
{
	pthread_t thread_id;
	pthread_attr_t attr;
	int res = 0;
	
	/*初始化线程属性*/
	res = pthread_attr_init(&attr);
	if (res != 0)
	{
		LOG_ERROR("pthread_attr_init fail\r\n");
		return -1;
	}

	/*设置为分离线程*/
	res = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
	if (res != 0)
	{
		LOG_ERROR("pthread_attr_setdetachstate failed\r\n");
		pthread_attr_destroy(&attr);
		return -1;
	}

	/*创建线程，线程为test_thread函数*/
	res = pthread_create(&thread_id, &attr, app_gui_poll_thread, NULL);
	if(res != 0)
	{
		LOG_ERROR("create thread fail\r\n");
		pthread_attr_destroy(&attr);
		return -1;
	}

	/*销毁线程属性对象*/
	pthread_attr_destroy(&attr);

	return 0;
}

/***************************************************************
 * Name:	 app_gui_demo()
 * Input : NULL
 * Output: void
 * Return: void
 * Author: hwl
 * Revise: V1.0
 * Description: 一个按键和标签交互的例成
 ***************************************************************/
static void app_gui_demo(void)
{
	ui_init();

	ui_sysinfo_init();
}

/***************************************************************
 * Name:	 app_gui_init()
 * Input : NULL
 * Output: void
 * Return: void
 * Author: hwl
 * Revise: V1.0
 * Description: LVGL初始化
 ***************************************************************/
int app_gui_init(void)
{
	/*初始化LVGL*/
	lv_init();
	lv_port_disp_init();
	lv_port_indev_init();

	if(app_gui_tick_init() < 0)
	{
		return -1;
	}

	if(app_gui_poll_init() < 0)
	{
		return -1;
	}

	app_gui_demo();

	return 0;
}
